package com.example.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.blog.base.CurrentAccount;
import com.example.blog.common.StatusEnums;
import com.example.blog.entity.Reply;
import com.example.blog.entity.SysUser;
import com.example.blog.entity.dto.reply.AddReplyDTO;
import com.example.blog.entity.dto.reply.PageQueryDTO;
import com.example.blog.entity.vo.comment.ReplyDetailIfoVO;
import com.example.blog.mapper.ReplyMapper;
import com.example.blog.service.ReplyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.blog.service.SysUserService;
import com.example.blog.util.BeanUtils;
import com.example.blog.util.CurrentUserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 二级评论表 服务实现类
 * </p>
 *
 * @author wlh
 * @since 2024-01-10
 */
@Service
public class ReplyServiceImpl extends ServiceImpl<ReplyMapper, Reply> implements ReplyService {


    @Autowired
    @Lazy
    private SysUserService sysUserService;

    @Override
    public Long getNewCountByUser(Long userId) {
        LambdaQueryWrapper<Reply> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(Reply::getTargetUserId, userId)
                .eq(Reply::getIsRead, StatusEnums.NOT_READ.getValue());

        return count(queryWrapper);
    }

    @Override
    public void delByComment(Long commentId) {
        LambdaUpdateWrapper<Reply> updateWrapper = new LambdaUpdateWrapper<>();

        updateWrapper.eq(Reply::getCommentId, commentId);

        remove(updateWrapper);
    }

    @Override
    public Long add(AddReplyDTO addReplyDTO) {
        Reply reply = BeanUtils.copyProperties(addReplyDTO, Reply.class);
        reply.setIsRead(StatusEnums.NOT_READ.getValue());

        save(reply);

        return reply.getId();
    }

    @Override
    public Page<ReplyDetailIfoVO> pageList(PageQueryDTO pageQueryDTO) {

        LambdaQueryWrapper<Reply> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(Reply::getCommentId, pageQueryDTO.getCommentId())
                .orderByDesc(Reply::getCreatedTime);

        Page<Reply> page = new Page<>(pageQueryDTO.getPageCurrent(), pageQueryDTO.getPageSize());

        page(page, queryWrapper);

        List<Reply> records = page.getRecords();

        if (records.isEmpty()) {
            return BeanUtils.copyPage(page, ReplyDetailIfoVO.class);
        }

        Set<Long> uids = new HashSet<>();
        records
                .forEach(reply -> {
                    uids.add(reply.getCreatedBy());
                    uids.add(reply.getTargetUserId());
                });



        Map<Long, SysUser> userMap = sysUserService.listByIds(uids)
                .stream()
                .collect(Collectors.toMap(SysUser::getId, Function.identity()));

        List<ReplyDetailIfoVO> list = records.stream()
                .map(reply -> {
                    ReplyDetailIfoVO replyDetailIfoVO = BeanUtils.copyProperties(reply, ReplyDetailIfoVO.class);
                    SysUser targetUser = userMap.get(replyDetailIfoVO.getTargetUserId());
                    if (targetUser != null) {
                        replyDetailIfoVO.setTargetNickname(targetUser.getNickname());
                        replyDetailIfoVO.setTargetAvatar(targetUser.getAvatar());
                        replyDetailIfoVO.setTargetUsername(targetUser.getUsername());
                    }
                    SysUser sysUser = userMap.get(replyDetailIfoVO.getCreatedBy());
                    if (sysUser != null) {
                        replyDetailIfoVO.setSendNickname(sysUser.getNickname());
                        replyDetailIfoVO.setSendAvatar(sysUser.getAvatar());
                        replyDetailIfoVO.setSendUsername(sysUser.getUsername());
                        replyDetailIfoVO.setSendUserId(sysUser.getId());
                    }
                    return replyDetailIfoVO;
                }).toList();
        Page<ReplyDetailIfoVO> replyDetailIfoVOPage = BeanUtils.copyPage(page, ReplyDetailIfoVO.class);
        replyDetailIfoVOPage.setRecords(list);
        return replyDetailIfoVOPage;
    }

    @Override
    public void read() {

        CurrentAccount user = CurrentUserUtils.currentUser();

        LambdaUpdateWrapper<Reply> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Reply::getTargetUserId, user.getId())
                .eq(Reply::getIsRead, StatusEnums.NOT_READ.getValue())
                .set(Reply::getIsRead, StatusEnums.READ.getValue());
        update(updateWrapper);
    }
}
